<?php
class RubrosController extends AppController {

    public $helpers = array('Html', 'Form');

	var $components = array('Auth','Paginator');
	
	function beforeFilter()
	{
		$this->Auth->userModel = 'User';
		$this->Auth->fields = array('username' => 'username', 'password' => 'password');
		$this->Auth->loginAction = array('admin' => false, 'controller' => 'pages', 'action' => 'display');
		$this->Auth->loginRedirect = array('controller' => 'productos', 'action' => 'index');
	}
	
    public function admin_index() {
	
    	$this->Paginator->settings = array( 'limit' => 8);
        $this->set('rubros', $this->paginate('Rubro'));
    }
	
	public function admin_view($id = null) {
	
        $this->set('rubro', $this->Rubro->findById($id));
    }
	
	 public function admin_add() {
	 
        if ($this->request->is('post')) {
		
            if ($this->Rubro->save($this->request->data)) {
			
                $this->Session->setFlash('El rubro ha sido agregado.');
                $this->redirect(array('action' => 'index'));
            } 
			else {
			
                $this->Session->setFlash('No se ha podido agregar el rubro.');
            }
        }
    }
	
	public function admin_edit($id = null) {
	
		if ($this->request->is('get')) {
			
			$this->request->data = $this->Rubro->findById($id);
		} 
		else {
		
			if ($this->Rubro->save($this->request->data)) {
				$this->Session->setFlash('El rubro ha sido actualizado.');
				$this->redirect(array('action' => 'index'));
			}
			else {
			
				$this->Session->setFlash('No se ha podido actualizar el rubro.');
			}
		}
	}
	
	public function admin_delete($id) {
	
		if ($this->request->is('get')) {
		
			throw new MethodNotAllowedException();
		}
		if ($this->Rubro->delete($id)) {
			
			$this->Session->setFlash('El rubro con id: ' . $id . ' ha sido borrado.');
			$this->redirect(array('action' => 'index'));
		}
	}	
}
?>